// <copyright file="MenuService.cs" company="Clever Software">
// Copyright (c) 2013, 2014 All Right Reserved, http://www.cleversoftware.by
//
// This source is subject to the Microsoft Permissive License.
// Please see the License.txt file for more information.
// All other rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// </copyright>
// <author>Clever Software</author>
// <email>info@cleversoftware.by</email>
// <date>2014-02-04</date> 
// <summary>CleverCMS source code</summary>

namespace CleverCMS.Cms.Services
{
    using System.Linq;
    using System.Collections.Generic;
    
    using Cms.Data;
    using Cms.Data.Entities;
    using Core.Infrastructure;

    public class MenuService
    {
        private readonly IMenuRepository m_menuRepo;
        private readonly AppCache m_appCache;           

        /// <summary>
        /// .Ctor
        /// </summary>
        /// <param name="menuRepo"></param>
        public MenuService(IMenuRepository menuRepo)
        {
            this.m_menuRepo = menuRepo;
            this.m_appCache = new AppCache();
        }
        /// <summary>
        /// Save menu item
        /// </summary>
        /// <param name="item"></param>
        public void SaveMenuItem(CmsMenuItem item)
        {
            m_menuRepo.SaveMenuItem(item);
            m_appCache.Remove("AdminMenus");
        }
        /// <summary>
        /// Return all menus
        /// </summary>
        /// <returns></returns>
        public IQueryable<CmsMenu> LoadMenues()
        {
            return m_menuRepo.LoadAll();
        }
        /// <summary>
        /// Return menu item
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public CmsMenuItem LoadMenuItem(int id)
        {
            return m_menuRepo.LoadMenuItem(id);
        }
        /// <summary>
        /// Return menu by name
        /// </summary>
        /// <param name="menuName"></param>
        /// <returns></returns>
        public CmsMenu LoadMenu(string menuName)
        {
            CmsMenu menu = m_menuRepo.LoadMenu(menuName);
           
            if (null != menu)
                RemoveNotVisibleMenuItems(menu);
            else
                menu = new CmsMenu();

            return menu;
        }
        /// <summary>
        /// Load menu by id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public CmsMenu LoadMenu(int id)
        {
            return m_menuRepo.Load(id);
        }
        /// <summary>
        /// Load menu items
        /// </summary>
        /// <returns></returns>
        public IQueryable<CmsMenuItem> LoadAllMenuItems()
        {
            return m_menuRepo.LoadAllItems();
        }
        /// <summary>
        /// Save menu
        /// </summary>
        /// <param name="menu"></param>
        public void SaveMenu(CmsMenu menu)
        {
            m_menuRepo.Save(menu);
            m_appCache.Remove("AdminMenus");

        }
        /// <summary>
        /// Remove invisible menu item 
        /// </summary>
        /// <param name="menu"></param>
        private void RemoveNotVisibleMenuItems(CmsMenu menu)
        {
            ICollection<CmsMenuItem> visibleItems = new List<CmsMenuItem>();

            foreach (CmsMenuItem item in menu.MenuItems)
            {
                if (item.Visible)
                {
                    visibleItems.Add(item);
                }
            }

            menu.MenuItems = visibleItems;
        }
        /// <summary>
        /// Remove menu by id
        /// </summary>
        /// <param name="id"></param>
        public void Remove(int id)
        {
            m_menuRepo.Delete(id);
            m_appCache.Remove("AdminMenus");
        }
        /// <summary>
        /// Rename menu
        /// </summary>
        /// <param name="id"></param>
        /// <param name="newName"></param>
        public void RenameMenuType(int id, string newName)
        {
            m_menuRepo.Rename(id, newName);
            m_appCache.Remove("AdminMenus");
        }
    }
}